/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.bean.SafetyMeetingBean;
import com.koron.purpose.service.api.SafetyMeetingService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "safetyMeeting", description = "安全会议接口")
@RestController
@RequestMapping("/safetyMeeting")
public class SafetyMeetingController {
    private final Logger logger = LoggerFactory.getLogger(SafetyMeetingController.class);

    @Autowired
    private SafetyMeetingService safetyMeetingService;

    @ApiOperation("删除安全会议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "安全会议id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteSafetyMeeting.htm", method = RequestMethod.POST)
    @Log(title = "删除安全会议节点", businessModule = "安全会议", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> deleteSafetyMeeting(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyMeetingService.deleteSafetyMeeting(id, user);
    }

    @ApiOperation("新增安全会议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobsTasksId", value = "工作任务ID",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "水司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingName", value = "会议名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTitle", value = "会议主题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "manager", value = "发起单位code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "managerName", value = "发起单位名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingUser", value = "会议发起人", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingUserName", value = "会议发起人名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTime", value = "会议时间(时间格式：yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTimeStop", value = "会议结束时间(时间格式：yyyy-MM-dd HH:mm:ss)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingAddress", value = "会议地点", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingJoinUsers", value = "会议与会人员(参数格式为JSON字符串，" +
                    "如( [{'code':'xxx','name':'xxx'},{'code':'xxx','name':'xxx'}] ))", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingAbsentUsers", value = "会议缺席人员(参数格式为JSON字符串，" +
                    "如( [{'code':'xxx','name':'xxx'},{'code':'xxx','name':'xxx'}] ))",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/insertSafetyMeeting.htm", method = RequestMethod.POST)
    @Log(title = "新增安全会议", businessModule = "安全会议", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> insertSafetyMeeting(@ApiIgnore String jobsTasksId, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                              @ApiIgnore SafetyMeetingBean bean, @RequestParam(value = "files", required = false) MultipartFile[] files) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyMeetingService.insert(bean, user, files, jobsTasksId);
    }

    @ApiOperation("查询安全会议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "安全会议id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/query.htm", method = RequestMethod.GET)
    public MessageBean<?> query(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyMeetingService.queryById(id);
    }

    @ApiOperation("修改安全会议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobsTasksId", value = "工作任务ID",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingName", value = "会议名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTitle", value = "会议主题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "manager", value = "发起单位code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "managerName", value = "发起单位名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingUser", value = "会议发起人", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingUserName", value = "会议发起人名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTime", value = "会议时间(时间格式：yyyy-MM-dd hh:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTime", value = "会议时间(时间格式：yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTimeStop", value = "会议结束时间(时间格式：yyyy-MM-dd HH:mm:ss)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingJoinUsers", value = "会议与会人员(参数格式为JSON字符串，" +
                    "如( [{'code':'xxx','name':'xxx'},{'code':'xxx','name':'xxx'}] ))", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingAbsentUsers", value = "会议缺席人员(参数格式为JSON字符串，" +
                    "如( [{'code':'xxx','name':'xxx'},{'code':'xxx','name':'xxx'}] ))",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingRecord", value = "会议记录(参数格式为JSON字符串，" +
                    "如( [{'filename':'xxx','fileUrl':'xxx'},{'filename':'xxx','fileUrl':'xxx'}] ))",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/updateSafetyMeeting.htm", method = {RequestMethod.POST})
    @Log(title = "修改安全会议", businessModule = "安全会议", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> updateSafetyMeeting(@ApiIgnore String jobsTasksId, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                              @ApiIgnore SafetyMeetingBean bean, @RequestParam(value = "files", required = false) MultipartFile[] files) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyMeetingService.updateSafetyMeeting(bean, user, files, jobsTasksId);
    }

   /* @ApiOperation("修改安全会议-会议记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingRecord", value = "会议记录(参数格式为JSON字符串，" +
                    "如( [{'filename':'xxx','fileUrl':'xxx'},{'filename':'xxx','fileUrl':'xxx'}] ))",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/updateSafetyMeetingRecord.htm", method = {RequestMethod.POST})
    @Log(title = "修改安全会议-会议记录", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> updateSafetyMeetingRecord(@ApiIgnore String id, @ApiIgnore String meetingRecord,
                                                    @RequestParam(value = "file", required = false) MultipartFile[] files) {
        return safetyMeetingService.updateSafetyMeetingRecord(id, meetingRecord, files);
    }*/

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "managerName", value = "发起单位",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingUserName", value = "发起人员",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTimeStart", value = "会议时间开始(日期格式：yyyy-MM-dd HH:mm:ss)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTimeStop", value = "会议时间结束(日期格式：yyyy-MM-dd HH:mm:ss)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/querySafetyMeeting.htm", method = RequestMethod.GET)
    public MessageBean<?> querySafetyMeeting(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String corpCode, @ApiIgnore String managerName, @ApiIgnore String meetingUserName,
                                             @ApiIgnore String meetingTimeStart, @ApiIgnore String meetingTimeStop,
                                             @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        page = (page == null || page < 1) ? 1 : page;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        MessageBean<DataBean<Map>> msg;
        msg = safetyMeetingService.querySafetyMeeting(
                corpCode, managerName, meetingUserName,
                meetingTimeStart, meetingTimeStop,
                page, pageSize
        );
        msg.getData().setPageNo(page);
        msg.getData().setPageSize(pageSize);
        return msg;
    }

    /*@ApiOperation("删除文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileUrl", value = "文件URL", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteFile.htm", method = RequestMethod.POST)
    @Log(title = "文件删除", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> deleteFile(@ApiIgnore String fileUrl) {
        return safetyMeetingService.deleteFile(fileUrl);
    }*/

    @ApiOperation("导出安全会议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "managerName", value = "发起单位",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingUserName", value = "发起人员",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTimeStart", value = "会议时间开始",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingTimeStop", value = "会议时间结束",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/exportSafetyMeeting.htm", method = RequestMethod.GET)
    @Log(title = "导出安全会议", businessModule = "安全会议", businessManipulation = BusinessManipulation.EXPORT)
    public HttpEntity<?> exportSafetyMeeting(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String corpCode, @ApiIgnore String managerName, @ApiIgnore String meetingUserName,
                                             @ApiIgnore String meetingTimeStart, @ApiIgnore String meetingTimeStop) {
        if (null == user) {
            return null;
        }
        List<Map> list = safetyMeetingService.querySafetyMeetings(corpCode, managerName, meetingUserName, meetingTimeStart, meetingTimeStop);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File file = new File("static/excel/SafetyMeetingExport.xlsx");
        try (
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/SafetyMeetingExport.xlsx");
        ) {
            FileUtils.copyInputStreamToFile(inputStream, file);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("安全会议", file, "SafetyMeetingExport.btl", map);
    }
}
